package com.juc.learn.cyclicbarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * ClassName:CyclicBarrierDemo
 * Package:com.juc.learn.cyclicbarrier
 * Description:循环栅栏
 *
 * @Author:@郭先森
 * @Create:2025/7/27 - 21:15
 * @Version:v1.0
 */

//集齐七颗龙珠就可以召唤神龙
public class CyclicBarrierDemo {

    private static final Integer NUMBER=7;
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(NUMBER,()->{
            System.out.println("集齐七颗龙珠就可以召唤神龙");
        });
        //集齐七颗龙珠的过程
        for (int i = 0; i < 7; i++) {
            new Thread(()->{
                try {
                    System.out.println("集齐了第"+Thread.currentThread().getName()+"星龙珠");
                    //等待
                    cyclicBarrier.await();//直到有NUMBER个线程调用了这个方法，就会运行Runnable接口中的方法
                                          //如果循环数是6，那么就会阻塞
                } catch (Exception e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }
    }
}
